package com.ssy.lingxi.product.controller.pc.price;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.price.GoodsPriceQueryRequest;
import com.ssy.lingxi.product.api.model.request.price.PriceAddRequest;
import com.ssy.lingxi.product.api.model.request.price.PriceHistoryQueryRequest;
import com.ssy.lingxi.product.api.model.response.price.GoodsPriceHistoryResponse;
import com.ssy.lingxi.product.api.model.response.price.GoodsPriceResponse;
import com.ssy.lingxi.product.api.model.response.price.PriceHistoryResponse;
import com.ssy.lingxi.product.entity.Goods;
import com.ssy.lingxi.product.entity.price.GoodsPrice;
import com.ssy.lingxi.product.entity.price.GoodsPriceHistory;
import com.ssy.lingxi.product.service.price.IPriceManagementService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @menu: 价格管理模块
 * 价格管理
 * @author lxc
 * @version 2.0.0
 * @date 2022/3/25
 */
@RestController
@RequestMapping("/product/price/management")
public class PriceManagementController extends BaseController {

    @Resource
    private IPriceManagementService priceManagementService;

    /**
     * 查询物料价格列表
     * @param pageVO 分页实体
     * @param goodsPriceQueryRequest 物料价格查询类
     */
    @RequestMapping(value = "/getGoodsPriceList", method = RequestMethod.GET)
    public Wrapper<PageData<GoodsPriceResponse>> getGoodsPriceList(PageVO pageVO, GoodsPriceQueryRequest goodsPriceQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<GoodsPrice> result = priceManagementService.getGoodsPriceList(sysUser, pageVO, goodsPriceQueryRequest);
        if (result.getTotalElements() <= 0){
            return Wrapper.success(new PageData<>(0L, new ArrayList<>()));
        }
        List<GoodsPriceResponse> resultList = result.getContent().stream().map(price -> {
            GoodsPriceResponse response = BeanUtil.copyProperties(price, GoodsPriceResponse.class);
            Goods goods = price.getGoods();
            BeanUtil.copyProperties(goods, response);
            response.setGoodsCode(goods.getCode());
            response.setCode(price.getCode());
            response.setId(price.getId());
            return response;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 物料价格历史列表 - 新增市场价
     * @param addRequest 价格信息
     */
    @RequestMapping(value = "/addGoodsPrice", method = RequestMethod.GET)
    public Wrapper<Void> AddGoodsPrice(@Validated PriceAddRequest addRequest) {
        return priceManagementService.AddGoodsPrice(addRequest);
    }

    /**
     * 查询物料价格历史列表 - 查看单个物料历史价格详情
     * @param pageVO 分页实体
     * @param queryRequest 物料价格历史信息查询类
     */
    @RequestMapping(value = "/getGoodsPriceHistory", method = RequestMethod.GET)
    public Wrapper<PageData<PriceHistoryResponse>> getGoodsPriceHistory(PageVO pageVO, PriceHistoryQueryRequest queryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Wrapper<PageData<GoodsPriceHistory>> result = priceManagementService.getGoodsPriceHistory(sysUser, pageVO, queryRequest);
        List<PriceHistoryResponse> resultList = this.modelMapper.map(result.getData().getData(), new TypeToken<List<PriceHistoryResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getData().getTotalCount(), resultList));
    }

}
